<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input v-model="queryParams.keyword" style="margin-bottom:10px" type="text" prefix-icon="el-icon-search" size="small" placeholder="输入员工姓名全员搜索" @input="cInput" />
        <!-- 树形组件 -->
        <el-tree
          ref="tree"
          node-key="id"
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="currentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="hExport">excel导出</el-button>
          <el-button size="mini" @click="excelD = true">excel导入</el-button>
          <!-- v-allow="'excel_export'" -->
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column align="center" label="头像">
            <template v-slot="{row}">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" alt="">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="{row}">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{row}">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="look(row.id)">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <!-- <el-pagination background layout="sizes, prev, pager, next" :total="total" :pagesize="queryParams.pagesize" :current-page="queryParams.page" /> -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination background layout="sizes, prev, pager, next" :total="total" :pagesize="queryParams.pagesize" :current-page="queryParams.page" />
          <!-- @current-change="change" @size-change="handleSizeChange"  -->
        </el-row></div>
    </div>
    <importExcel :excel-d.sync="excelD" @updategetdepts="getdepts" />
    <assignRoleVue ref="resRole" :c-id="cId" :show-dislog.sync="showDislog" />
  </div>
</template>

<script>
import { arr2tree } from '@/utils/index'
import { getDepartment } from '@/api/department'
import { exportEmployee, getEmployeeList } from '@/api/employee'
import FileSaver from 'file-saver'
import importExcel from './components/import-excel.vue'

import assignRoleVue from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: {
    importExcel,
    assignRoleVue
  },
  data() {
    return {
      cId: -1,
      excelD: false,
      showDislog: false,
      queryParams: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      depts: [], // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      total: 0,
      list: []
    }
  },
  created() {
    this.getdepts()
  },
  methods: {
    look(id) {
      this.cId = id
      this.showDislog = true
      this.$nextTick(() => {
        this.$refs.resRole.getRoleBy()
      })
    },
    async hExport() {
      const res = await exportEmployee()
      //
      FileSaver.saveAs(res, '员工信息.xlsx')
    },
    cInput() {
      clearTimeout(this.timerId)
      this.timerId = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 500)
    },
    async getEmployeeList() {
      const res = await getEmployeeList(this.queryParams)
      //
      this.total = res.data.total
      this.list = res.data.rows
    },
    async getdepts() {
      const res = await getDepartment()
      const ar = arr2tree(res.data, 0)
      this.depts = ar
      const id = ar[0].id
      this.queryParams.departmentId = id
      this.getEmployeeList()
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([id])
        this.$refs.tree.setCurrentKey(id)
      })
    },
    currentChange(data, node) {
      //
      this.queryParams.departmentId = data.id
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
<style >
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
    color: #276FFB;
    background-color: #f0f4ff;
}
</style>
